import java.util.Arrays;

public class TestHeap {
    public int[] elem;
    public int useSize;


    public TestHeap() {
        this.elem = new int[10];
    }

    public void initElem(int[] array) {
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            useSize++;
        }
    }

    /*public void createHeap() {
        for (int parent = (useSize - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(parent, useSize);
        }
    }

    private void siftDown(int parent, int useSize) {
        int child = parent * 2 + 1;
        while (child < useSize) {
            if (child + 1 < useSize && elem[child] < elem[child + 1]) {
                child++;
            }
            if (elem[parent] < elem[child]) {
                Swap(elem, parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }*/

    public void createHeap() {
        for (int parent = (useSize - 1-1) / 2; parent >= 0; parent--) {
            siftDown(parent, useSize);
        }
    }

    private void siftDown(int parent, int useSize) {
        int child = parent * 2 + 1;
        while (child < useSize) {
            if (child + 1 < useSize && elem[child] < elem[child + 1]) {
                child++;
            }
            if (elem[parent] < elem[child]) {
                Swap(elem, parent, child);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    private void Swap(int[] elem, int parent, int child) {
        int tmp = elem[parent];
        elem[parent] = elem[child];
        elem[child] = tmp;
    }

    public void heapSort(){
        int end = useSize-1;
        while (end>0){
            Swap(elem,0,end);
            end--;
            siftDown(0,end);
            //可以这样写吗？
            //siftDown(0,end);
            //end--;
        }
    }

    public void poll() {
        if (isEmpty()) {
            return;
        }
        int parent = 0;
        Swap(elem, parent, useSize - 1);
        useSize--;
        siftDown(parent, useSize);
    }

    public boolean isEmpty() {
        return useSize == 0;
    }

    public void offer(int val) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[useSize] = val;
        siftUp(elem, 0, useSize);
        useSize++;
    }

    private void siftUp(int[] elem, int parent, int child) {
        parent = (child - 1) / 2;
        while (parent >= 0) {
            if (elem[child] > elem[parent]) {
                Swap(elem, parent, child);
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }

    private boolean isFull() {
        return useSize == elem.length;
    }



    public void print(){
        for (int i = 0; i < elem.length; i++) {
            System.out.println(elem[i]);
        }
    }

}
